package com.cash.loan.modular.cash.service.impl;

import com.cash.loan.common.upload.QiniuService;
import com.cash.loan.modular.cash.dao.*;
import com.cash.loan.modular.cash.model.LoanProduct;
import com.cash.loan.modular.cash.model.ProductAdPicture;
import com.cash.loan.modular.cash.service.ProductIndexService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页接口处理类
 *
 * @author Administrator
 * @Date 2018-07-09 9:11
 */
@Service
public class ProductIndexServiceImpl extends CommonService implements ProductIndexService {

    private static final Integer UI_TYPE = 2;

    @Autowired
    private ProductAdPictureMapper adPictureMapper;

    @Autowired
    private LoanProductMapper productMapper;

    @Autowired
    private LoanProductDetailMapper detailMapper;

    @Autowired
    private QiniuService qiniuService;

    @Autowired
    private LoanCateMapper cateMapper;

    @Autowired
    private LoanRecommendMapper recommendMapper;

    /**
     * 得到首页轮播图片
     * @return
     */
    @Override
    public List<Map<String,Object>> getIndexHeader(String type,Integer uiType) {
        List<Map<String,Object>> adResultList = new ArrayList<>();
        List<ProductAdPicture> adPictureList = adPictureMapper.getUsedAdPicture(type);

        if(adPictureList == null || adPictureList.size() == 0) {
            return new ArrayList<>(1);
        }

        if(UI_TYPE.equals(uiType)) {
            adPictureList = adPictureList.stream()
                    .filter(productAdPicture -> !isDownloadProductAd(productAdPicture.getProductId()))
                    .collect(Collectors.toList());
        }

        adPictureList.forEach(adPicture -> {
            Map<String,Object> map = new HashMap<>(4);
            map.put("productId",adPicture.getProductId());
            map.put("adImgUrl",qiniuService.getUrl(adPicture.getAdImgUrl()));
            if(StringUtils.isNoneBlank(adPicture.getAdProductH5Url())) {
                map.put("adProductH5Url",adPicture.getAdProductH5Url());
            } else {
                 map.put("adProductH5Url","");
            }
            adResultList.add(map);
        });
        return adResultList;
    }

    /**
     * 新口子展示
     * @return
     */
    @Override
    public List<Map<String, Object>> getNewProduct(Integer uiType) {
        List<LoanProduct> loanProductList = productMapper.getNewLimitProduct();

        if(loanProductList == null && loanProductList.size() == 0) {
            return new ArrayList<>(1);
        }

        if(UI_TYPE.equals(uiType)) {
            loanProductList = loanProductList.stream()
                    .filter(loanProduct -> Integer.valueOf(0).equals(loanProduct.getDownloadFlag()))
                    .collect(Collectors.toList());
        }

        List<Map<String,Object>> loanProductResultList = new ArrayList<>();
        loanProductList.stream().forEach(loanProduct -> {
            Map<String,Object> map = new HashMap<>(8);
            map.put("productId",loanProduct.getId());
            map.put("productName",loanProduct.getProductName());
            map.put("productIconUrl",qiniuService.getUrl(loanProduct.getProductIconUrl()));
            map.put("productSellPoint",loanProduct.getProductSellPoint());
            loanProductResultList.add(map);
        });
        return loanProductResultList;
    }

    /**
     * 得到热门口子
     * @return
     */
    @Override
    public List<Map<String, Object>> getHotProduct(Integer uiType) {
        List<Map<String,Object>> hotProductMapList = new ArrayList<>();
        List<LoanProduct> hotLoanProductList = productMapper.getHotProduct();

        if(hotLoanProductList == null || hotLoanProductList.size() == 0) {
            return new ArrayList<>(1);
        }

        if(UI_TYPE.equals(uiType)) {
            hotLoanProductList = hotLoanProductList.stream()
                    .filter(loanProduct -> Integer.valueOf(0).equals(loanProduct.getDownloadFlag()))
                    .collect(Collectors.toList());
        }

        hotProductMapList = dealProductInfo(hotLoanProductList);
        return hotProductMapList;
    }

    @Override
    public List<Map<String, Object>> getProductCate() {
        List<Map<String,Object>> cateList = cateMapper.selectCateLimit();
        cateList.stream().forEach(map -> {
            if(StringUtils.isNoneBlank((String) map.get("iconPath"))) {
                map.put("iconPath",qiniuService.getUrl((String) map.get("iconPath")));
            }
        });
        return cateList;
    }

    @Override
    public Map<String, Map<String, Object>> getRecommendList(Integer uiType) {
        Map<String,Map<String,Object>> result = new HashMap<>(6);
        Map<String,Object> firstField = recommendMapper.getFieldLimitByFieldId(1);
        firstField = getStringObjectMap(uiType, firstField);
        Map<String,Object> secondField = recommendMapper.getFieldLimitByFieldId(2);
        secondField = getStringObjectMap(uiType, secondField);
        Map<String,Object> thirdField = recommendMapper.getFieldLimitByFieldId(3);
        thirdField = getStringObjectMap(uiType, thirdField);
        result.put("firstField",firstField);
        result.put("secondField",secondField);
        result.put("thirdField",thirdField);
        return result;
    }

    /**
     * 处理各个栏目的uiType审核的问题
     * @param uiType
     * @param field
     * @return
     */
    private Map<String, Object> getStringObjectMap(Integer uiType, Map<String, Object> field) {
        if(field != null) {
            if (UI_TYPE.equals(uiType)) {
                if(isDownloadProductAd((Long) field.get("productId"))) {
                    field = null;
                }
            }
            if(field != null) {
                field = dealRecommendResult(field);
            }
        }
        return field;
    }

    /**
     * 处理推荐返回结果
     * @param map
     * @return
     */
    public Map<String,Object> dealRecommendResult(Map<String,Object> map) {
        if(StringUtils.isBlank((String) map.get("title"))) {
            map.put("title","");
        }
        if(StringUtils.isBlank((String) map.get("recommend"))) {
            map.put("recommend","");
        }
        if(StringUtils.isBlank((String) map.get("productIconUrl"))) {
            map.put("productIconUrl","");
        } else {
            map.put("productIconUrl",qiniuService.getUrl((String) map.get("productIconUrl")));
        }
        return map;
    }

    /**
     * 处理产品信息
     * @param loanProductList
     * @return
     */
    protected List<Map<String,Object>> dealProductInfo(List<LoanProduct> loanProductList) {
        List<Map<String,Object>> productMapList = new ArrayList<>();
        loanProductList.stream().forEach(loanProduct -> {
            Map<String,Object> map = new HashMap<>();
            map.put("productId",loanProduct.getId());
            map.put("productName",loanProduct.getProductName());
            map.put("productIconUrl",qiniuService.getUrl(loanProduct.getProductIconUrl()));
            map.put("productH5Url",detailMapper.getH5Url(loanProduct.getProductId()));
            map.put("productSellPoint",loanProduct.getProductSellPoint());
            map.put("dayLoanRate",loanProduct.getDayLoanRate());
            StringBuilder dateTerm = new StringBuilder("");
            if(loanProduct.getMinLoanTerm() > 30) {
                dateTerm.append(loanProduct.getMinLoanTerm() / 30).append("月-");
            } else {
                dateTerm.append(loanProduct.getMinLoanTerm()).append("天-");
            }
            if(loanProduct.getMaxLoanTerm() > 30) {
                dateTerm.append(loanProduct.getMaxLoanTerm() / 30).append("月");
            } else {
                dateTerm.append(loanProduct.getMaxLoanTerm()).append("天");
            }
            map.put("loanTerm",dateTerm.toString());
            map.put("maxLoanAmount",loanProduct.getMaxLoanAmount());
            map.put("minLoanAmount",loanProduct.getMinLoanAmount());
            productMapList.add(map);
        });
        return productMapList;
    }
}
